/*
 * Copyright (c) 2023 huipei.x
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cn.xphsc.kubernetes.core.executor;


import cn.xphsc.kubernetes.core.query.DeleteOptionsBody;
import cn.xphsc.kubernetes.core.query.NamespaceQuery;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.apis.AppsV1Api;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import org.apache.commons.lang3.StringUtils;

/**
 * {@link }
 * @author <a href="xiongpeih@163.com">huipei.x</a>
 * @description: Delete Executor
 * @since 1.0.0
 */
public class DeleteNamespaceExecutor<T> extends AbstractExecutor<T> {
    private String deleteOptionName;
    private  T entity;
    private  CoreV1Api  coreV1Api;
    private DeleteOptionsBody deleteOptionsBody;
    private AppsV1Api appsv1api;
    public DeleteNamespaceExecutor(ApiClient client, DeleteOptionsBody deleteOptionsBody, DeleteOptionMode deleteOptionMode) {
        super(client);
        this.deleteOptionName=deleteOptionMode.name();
        this.coreV1Api=new CoreV1Api(this.getClient());
        this.appsv1api=new AppsV1Api(this.getClient());
        this.deleteOptionsBody=deleteOptionsBody;
    }

    @Override
    protected  T doExecute() {
        if (StringUtils.isNotBlank(deleteOptionName)) {
            switch (deleteOptionName){
                case "NS":
                    try {
                        entity = (T) coreV1Api.deleteNamespace(deleteOptionsBody.name(),deleteOptionsBody.pretty(),deleteOptionsBody.dryRun(),deleteOptionsBody.gracePeriodSeconds(),deleteOptionsBody.orphanDependents(),deleteOptionsBody.propagationPolicy(),deleteOptionsBody.body());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "SERVICE":
                    try {
                        entity = (T) coreV1Api.deleteNamespacedService(deleteOptionsBody.name(),deleteOptionsBody.namespace(),deleteOptionsBody.pretty(),deleteOptionsBody.dryRun(),deleteOptionsBody.gracePeriodSeconds(),deleteOptionsBody.orphanDependents(),deleteOptionsBody.propagationPolicy(),deleteOptionsBody.body());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "DEPLY":
                    try {
                        entity = (T) appsv1api.deleteNamespacedDeployment(deleteOptionsBody.name(), deleteOptionsBody.namespace(), deleteOptionsBody.pretty(), deleteOptionsBody.dryRun(), deleteOptionsBody.gracePeriodSeconds(), deleteOptionsBody.orphanDependents(), deleteOptionsBody.propagationPolicy(),deleteOptionsBody.body());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "SECRET":
                    try {
                        entity = (T) coreV1Api.deleteNamespacedSecret(deleteOptionsBody.name(),deleteOptionsBody.namespace(),deleteOptionsBody.pretty(),deleteOptionsBody.dryRun(),deleteOptionsBody.gracePeriodSeconds(),deleteOptionsBody.orphanDependents(),deleteOptionsBody.propagationPolicy(),deleteOptionsBody.body());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "CONFIGMAP":
                    try {
                        entity = (T) coreV1Api.deleteNamespacedConfigMap(deleteOptionsBody.name(),deleteOptionsBody.namespace(),deleteOptionsBody.pretty(),deleteOptionsBody.dryRun(),deleteOptionsBody.gracePeriodSeconds(),deleteOptionsBody.orphanDependents(),deleteOptionsBody.propagationPolicy(),deleteOptionsBody.body());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "POD":
                    try {
                        entity = (T) coreV1Api.deleteNamespacedPod(deleteOptionsBody.name(),deleteOptionsBody.namespace(),deleteOptionsBody.pretty(),deleteOptionsBody.dryRun(),deleteOptionsBody.gracePeriodSeconds(),deleteOptionsBody.orphanDependents(),deleteOptionsBody.propagationPolicy(),deleteOptionsBody.body());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "ENDPOINTS":
                    try {
                        entity = (T) coreV1Api.deleteNamespacedEndpoints(deleteOptionsBody.name(),deleteOptionsBody.namespace(),deleteOptionsBody.pretty(),deleteOptionsBody.dryRun(),deleteOptionsBody.gracePeriodSeconds(),deleteOptionsBody.orphanDependents(),deleteOptionsBody.propagationPolicy(),deleteOptionsBody.body());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "SERVICEACCOUNT":
                    try {
                        entity = (T) coreV1Api.deleteNamespacedServiceAccount(deleteOptionsBody.name(),deleteOptionsBody.namespace(),deleteOptionsBody.pretty(),deleteOptionsBody.dryRun(),deleteOptionsBody.gracePeriodSeconds(),deleteOptionsBody.orphanDependents(),deleteOptionsBody.propagationPolicy(),deleteOptionsBody.body());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "PODTEMPLATE":
                    try {
                        entity = (T) coreV1Api.deleteNamespacedPodTemplate(deleteOptionsBody.name(),deleteOptionsBody.namespace(),deleteOptionsBody.pretty(),deleteOptionsBody.dryRun(),deleteOptionsBody.gracePeriodSeconds(),deleteOptionsBody.orphanDependents(),deleteOptionsBody.propagationPolicy(),deleteOptionsBody.body());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "NODE":
                    try {
                        entity = (T) coreV1Api.deleteNode(deleteOptionsBody.name(),deleteOptionsBody.pretty(),deleteOptionsBody.dryRun(),deleteOptionsBody.gracePeriodSeconds(),deleteOptionsBody.orphanDependents(),deleteOptionsBody.propagationPolicy(),deleteOptionsBody.body());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                case "PERSISTENTVOLUME":
                    try {
                        entity = (T) coreV1Api.deletePersistentVolume(deleteOptionsBody.name(),deleteOptionsBody.pretty(),deleteOptionsBody.dryRun(),deleteOptionsBody.gracePeriodSeconds(),deleteOptionsBody.orphanDependents(),deleteOptionsBody.propagationPolicy(),deleteOptionsBody.body());
                    } catch (ApiException e) {
                        e.printStackTrace();
                    }
                    break;
                default:

            }
        }
        return (T) entity;
    }
    public static enum DeleteOptionMode {
        /**
         *
         */
        NS,
        SERVICE,
        ENDPOINTS,
        DEPLY,
        SECRET,
        ASYNC,
        SERVICEACCOUNT,
        CONFIGMAP,
        POD,
        PODTEMPLATE,
        NODE,
        PERSISTENTVOLUME,
        AUTO;
        private DeleteOptionMode() {

        }
        private DeleteOptionMode(String name) {
            this.name=name;
        }
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}


